package com.bestescortbedsystem.controller.qianduan;

import com.bestescortbedsystem.entity.SesUserOrder;
import com.bestescortbedsystem.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * ClassName: UserMoneyController
 * Package: com.bestescortbedsystem.controller.qianduan
 *
 * @author: guanzepeng
 * @Date: 2021/2/24 10:50 星期三
 * @Description: 用户钱包Controller
 */
@RestController
@Api(tags = "用户钱包Controller")
@Slf4j
@RequestMapping("/money")
public class UserMoneyController {

	@Resource
	private UserService userService;

	/**
	 * 此接口可以查询用户的基本信息
	 * @param sesUserOrder 可以输入指定的条件来按条件查询
	 * @param pageNum 分页 默认为1
	 * @param pageSize 分页 默认为5
	 * @return 返回值是包含了用户的订单信息以及钱包信息还有用户的基本信息
	 */
	@GetMapping(value = "find")
	@ApiOperation(value = "此接口可以查询用户的基本信息", response = Map.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "startMoney", value = "根据订单金额进行区间查询 起始金额", required = false, defaultValue = "0.0", dataTypeClass = Float.class),
			@ApiImplicitParam(name = "endMoney", value = "结束金额", required = false, defaultValue = "0.0", dataTypeClass = Float.class),
			@ApiImplicitParam(name = "pageNum", value = "进行分页从第几条数据开始", required = false, defaultValue = "1", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "pageSize", value = "每页显示几条数据", required = false, defaultValue = "5", dataTypeClass = Integer.class)
	})
	public Map<String, Object> findByMoney( HttpSession session,
	                                       SesUserOrder sesUserOrder,
	                                       @RequestParam(defaultValue = "0") String startMoney,
	                                       @RequestParam(defaultValue = "0") String endMoney,
	                                       @RequestParam(defaultValue = "1") Integer pageNum,
	                                       @RequestParam(defaultValue = "5") Integer pageSize){
		//获取用户ID
//		String userId = (String) session.getAttribute("userId2");
//		if (userId == null) {
//			return null;
//		}
		float endMoney1 = 0f;
		float startMoney1 = 0f;
		if(!("0".equals(startMoney)) && !("0".equals(endMoney))){
			startMoney1 = Float.parseFloat(startMoney);
			endMoney1 = Float.parseFloat(endMoney);
		}
		Map<String, Object> user = userService.findUser(31L, sesUserOrder, startMoney1, endMoney1, pageNum, pageSize);
		return user;
	}

	/**
	 * 调用此接口会进行钱包的一系列操作
	 * @param money 根据调用类型不同来决定传入的时用户钱包金额还是押金
	 * @param i 传入数字1调用充值钱包功能,传入数字2调用充值押金功能,传入数字3调用退还押金功能
	 * @return 返回值会根据调用数据类型不同来确定返回的数据
	 */
	@PostMapping(value = "money")
	@ApiOperation(value = "调用此接口会进行钱包的一系列操作", response = String.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "money", value = "根据调用类型不同来决定传入的时用户钱包金额还是押金", required = false, defaultValue = "0", dataTypeClass = String.class),
			@ApiImplicitParam(name = "i", value = "传入数字1调用充值钱包功能,传入数字2调用充值押金功能,传入数字3调用退还押金功能", required = true, dataTypeClass = Integer.class)
	})
	public String money( HttpSession session, @RequestParam(defaultValue = "0") String money, Integer i){
		String userId = (String) session.getAttribute("userId2");
		Float moneys = Float.valueOf(money);
		if(i == 1) {
			//调用充值钱包方法
			Float aFloat = userService.updateAddMoney(moneys, Long.valueOf(userId));
			return String.valueOf(aFloat);
		}else if(i == 2) {
			//调用充值押金方法
			Float aFloat = userService.updateDeposit(moneys, Long.valueOf(userId));
			return String.valueOf(aFloat);
		}else if(i == 3) {
			//调用退还押金方法
			Boolean b = userService.depositRefund(Long.valueOf(userId));
			return String.valueOf(b);
		}
		return "false";
	}


}
